#!/bin/sh

# SPDX-License-Identifier: Apache-2.0 OR MIT
#
# SPDX-FileCopyrightText: Copyright 2021 Micron Technology, Inc.

if [ -n "${HSE_GIT_HOOK_POST_CHECKOUT_SKIP}" ]; then
    exit 0
fi

branch=$(git branch --show-current)

for s in hse-java hse-python; do
    if git -C "subprojects/$s" checkout "$branch" > /dev/null 2>&1; then
        upstream=$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)")
        if [ -n "$upstream" ]; then
            remote="${upstream%%/*}"
            branch="${upstream#*/}"
            echo "Updating $s..."
            git -C "subprojects/$s" pull "$remote" "$branch" > /dev/null 2>&1
        fi
    fi
done
